"""赠品功能"""


from odoo import models, api, fields
from odoo.tools import float_compare
from odoo.exceptions import UserError, ValidationError


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    # 用于接入WMS
    use_wms = fields.Boolean('启用接口')
    api_url = fields.Char('WMS API地址')
    client_customerid = fields.Char('客户编码')
    app_token = fields.Char('客户接入号')
    app_key = fields.Char('客户接入KEY')
    app_secret = fields.Char('客户接入加密串')
    timestamp_type = fields.Char('时间格式')

    # 用于WMS接入ODOO
    odoo_token = fields.Char('ODOO 令牌')
    odoo_secret = fields.Char('ODOO 加密串')
    odoo_user_id = fields.Many2one('res.users', string="操作身份")

    # 帆软打印地址
    fanruan_print_address = fields.Char('帆软地址')

    @api.multi
    def set_values(self):

        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.use_wms', self.use_wms)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.api_url', self.api_url)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.client_customerid', self.client_customerid)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.app_token', self.app_token)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.app_key', self.app_key)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.app_secret', self.app_secret)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.timestamp_type', self.timestamp_type)

        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.odoo_token', self.odoo_token)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.odoo_secret', self.odoo_secret)
        self.env['ir.config_parameter'].sudo().set_param('nd.wms.api.odoo_user_id', self.odoo_user_id.id)

        self.env['ir.config_parameter'].sudo().set_param('nd.fanruan_print_address', self.fanruan_print_address)

        return super(ResConfigSettings, self).set_values()

    @api.model
    def get_values(self):
        res = super(ResConfigSettings, self).get_values()
        res['use_wms'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.use_wms', default=False) in ['True']
        res['api_url'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.api_url', default=False)
        res['client_customerid'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.client_customerid', default=False)
        res['app_token'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.app_token', default=False)
        res['app_key'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.app_key', default=False)
        res['app_secret'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.app_secret', default=False)
        res['timestamp_type'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.timestamp_type', default=False)

        res['odoo_token'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.odoo_token', default=False)
        res['odoo_secret'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.odoo_secret', default=False)
        res['odoo_user_id'] = self.env['ir.config_parameter'].sudo().get_param('nd.wms.api.odoo_user_id', default=False)

        res['fanruan_print_address'] = self.env['ir.config_parameter'].sudo().get_param('nd.fanruan_print_address', default=False)

        return res

    @api.multi
    def action_view_wms_api(self):
        """查看API接口列表"""
        action = self.env.ref('nd_wms.NDCheckAPIList').read()[0]
        return action


class APISet(models.Model):
    _name = 'nd.wms.api'
    _description = 'WMS系统接口'

    e_id = fields.Char('系统标识', unique=True)
    name = fields.Char('接口名称')
    api_method = fields.Char('接口方法')
    client_db = fields.Char('数据仓储DB编码')
    messageid = fields.Char('接口ID')

